package com.kuangstudy.thread.syn;

import sun.applet.Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @author Losca
 * @date 2021/7/19 21:21
 */
public class Demo1 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread( ()->{
                synchronized (list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();

        }
        /*一定要加Thread.sleep 要不然提前输出了 加了Thread.sleep输出10000*/
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());//9995 每次结果都不一样 说明不安全
    }
}
